<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像部分 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 输入部分 -->
      <div class="login_input">
        <el-form
          :model="loginForm"
          :rules="loginRules"
          label-width="0px"
          ref="loginFormRef"
        >
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              prefix-icon="iconfont icon-yonghu"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              prefix-icon="iconfont icon-mima"
              placeholder="请输入密码"
              show-password
            >
            </el-input>
          </el-form-item>
          <!-- 按钮 -->
          <el-form-item class="btns">
            <el-button type="success" round @click="login">登录</el-button>
            <el-button type="warning" round @click="resetLoginForm"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
// 导入请求接口
import { loginApi } from 'api/login'
export default {
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '111111'
      },
      // 校验规则部分开始
      loginRules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'change' },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'change' },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'change'
          }
        ]
      }
      // 校验规则结束
    }
  },
  methods: {
    // 重置方法
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    },
    // 登录方法
    login () {
      this.$refs.loginFormRef.validate(valid => {
        if (!valid) {
          return
        }
        loginApi(this.loginForm)
          .then(res => {
            this.loginSuccess(res)
          })
          .catch(err => this.requestFailed(err))
      })
    },
    // 成功后回调函数
    loginSuccess (res) {
      if (res.data.code) {
        return this.$message.error('登录失败！')
      }
      this.$message.success('登录成功！')
      // 保存token到session
      window.sessionStorage.setItem('token', res.data.token)
      // 保存状态到Vuex
      this.$store.commit('userInfo', this.loginForm)
      // 路由跳转
      this.$router.push('/home')
    },
    // 请求失败
    requestFailed (err) {
      // console.log(Object.keys(err.response).length)
      if (err.response !== undefined) {
        if (err.response.status === 403) {
          this.$message.error('用户名不存在！')
        } else if (err.response.status === 401) {
          this.$message.error('密码错误！')
        }
      } else {
        this.$message.error('服务器连接失败！')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background-color: #009973;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: auto;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #ffffff;
  border-radius: 6px;

  .avatar_box {
    height: 100px;
    width: 100px;
    border: 1px solid #eeeeee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #dddddd;
    margin: auto;
    transform: translateY(-50%);
    background-color: #ffffff;

    img {
      height: 100%;
      border-radius: 50%;
    }
  }

  .login_input {
    padding: 0 20px;
  }
}
</style>
